/*
 * @Description: 
 * @Version: 2.0
 * @Author: 
 * @Date: 2022-04-08 09:09:00
 * @LastEditors: cxc
 * @LastEditTime: 2022-04-08 13:22:08
 */
//本文件用于 请求 增加 修改 下拉选项，共其他组件实时使用
import myAxios from '../../../utils/myAxios';
import { message } from 'antd';
export function getOptionAction(data) {
  // console.log('111111',data);
  return { type: 'get', data }
}
export function addOptionAction(data) {
  return { type: 'add', data }
}
export function modifyOptionAction(data) {
  return { type: 'modify', data }
}
//获取
export function AsyncGetOptionAction() {
  return (dispatch) => {
    myAxios({
      method: 'get',
      url: '/customer/findAddLikeCustomerInitialization'
    })
      .then(res => {
        console.log(res.data.data);
        dispatch(getOptionAction(res.data.data))
      })
  }
}
//增加
export function AsyncAddOptionAction(type, value) {
  return (dispatch) => {

    let tempUrl
    switch (type) {
      case 'jobList':
        tempUrl = '/customer/customerPositon/add'
        break;
      case 'customerType':
        tempUrl = '/customer/customerType/add'
        break;
      case 'customerLevel':
        tempUrl = '/customer/customerLevel/add'
        break;
      case 'customerSource':
        tempUrl = '/customer/customerSource/add'
        break;
      default:
        break;
    }
    myAxios({
      method: 'post',
      url: tempUrl,
      params: { title: value }
    })
      .then(res => {
        console.log(res.data);
        if (res.data.code == 200) {
          message.success(res.data.msg);
          //新增成功后，再去调用获取所有下拉对的异步action，这样页面才能 实时更新下拉
          dispatch(AsyncGetOptionAction())
        } else {
          message.error(res.data.msg);
        }
      })

  }
}

//修改
export function AsyncModifyOptionAction(type, id,title) {
  return (dispatch) => {

    let tempUrl
    switch (type) {
      case 'jobList':
        tempUrl = '/customer/customerPositon/update'
        break;
      case 'customerType':
        tempUrl = '/customer/customerType/update'
        break;
      case 'customerLevel':
        tempUrl = '/customer/customerLevel/update'
        break;
      case 'customerSource':
        tempUrl = '/customer/customerSource/update'
        break;
      default:
        break;
    }
    myAxios({
      method: 'post',
      url: tempUrl,
      params: {id, title}
    })
      .then(res => {
        console.log(res.data);
        if (res.data.code == 200) {
          message.success(res.data.msg);
          //新增成功后，再去调用获取所有下拉对的异步action，这样页面才能 实时更新下拉
          dispatch(AsyncGetOptionAction())
        } else {
          message.error(res.data.msg);
        }
      })

  }
}

//删除
export function AsyncDeleteOptionAction(type,id) {
  return (dispatch) => {

    let tempUrl
    switch (type) {
      case 'jobList':
        tempUrl = '/customer/customerPositon/delete'
        break;
      case 'customerType':
        tempUrl = '/customer/customerType/delete'
        break;
      case 'customerLevel':
        tempUrl = '/customer/customerLevel/delete'
        break;
      case 'customerSource':
        tempUrl = '/customer/customerSource/delete'
        break;
      default:
        break;
    }
    myAxios({
      method: 'post',
      url: tempUrl,
      params: {id}
    })
      .then(res => {
        console.log(res.data);
        if (res.data.code == 200) {
          message.success(res.data.msg);
          //新增成功后，再去调用获取所有下拉对的异步action，这样页面才能 实时更新下拉
          dispatch(AsyncGetOptionAction())
        } else {
          message.error(res.data.msg);
        }
      })

  }
}